package com.bdqn.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.constant.Constants;
import com.bdqn.pojo.SysUser;
import com.bdqn.service.SysRoleService;
import com.bdqn.service.SysUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * SysUserController
 *
 * @author LILIBO
 * @since 2025/3/19
 */
@Controller
@RequestMapping("/user")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysRoleService sysRoleService;

    /**
     * 获取用户列表
     */
    @RequestMapping("/list")
    public String list(Model model, @RequestParam(required = false) String usrName, @RequestParam(required = false) Long roleId, @RequestParam(required = false, defaultValue = "1") Long pageIndex) {
        // 数据回显
        model.addAttribute("usrName", usrName);
        model.addAttribute("roleId", roleId);
        // 构建分页对象
        Page<SysUser> page = new Page<>(pageIndex, Constants.DEF_PAGE_SIZE);
        // 进行分页查询
        page = sysUserService.queryPage(usrName, roleId, page);
        // 将数据设置到作用域
        model.addAttribute("userPager", page);
        return "user/list";
    }

    /**
     * 新增用户
     */
    @GetMapping("/add")
    public String toAdd(Model model) {
        // 查询所有角色备选
        model.addAttribute("roles", sysRoleService.list());
        return "user/add";
    }

    /**
     * 去修改用户
     */
    @GetMapping("/edit")
    public String toEdit(Model model, Long usrId) {
        // 查询所有角色备选
        model.addAttribute("roles", sysRoleService.list());
        // 根据用户ID查询用户信息
        SysUser user = sysUserService.getById(usrId);
        model.addAttribute("user", user);
        return "user/edit";
    }

    /**
     * 检查用户名是否可用
     */
    @ResponseBody
    @GetMapping("/check")
    public Map check(String usrName, Long usrId) {
        Map<String, String> resultMap = new HashMap<>();
        String result = sysUserService.check(usrName, usrId);
        resultMap.put("result", result);
        return resultMap;
    }

    /**
     * 保存用户
     */
    @PostMapping("/save")
    public String doSave(HttpServletRequest request, HttpServletResponse response, SysUser sysUser) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        // 但需要保存的对象中有主键ID则为修改操作，否则为新增操作
        SysUser user = sysUserService.saveSysUser(sysUser);
        response.getWriter().print("<script>alert('保存" + (user != null ? "成功" : "失败") + "！');location='/user/list'</script>");
        return null;
    }

    /**
     * 删除用户
     */
    @ResponseBody
    @GetMapping("/del")
    public Map del(Long usrId) {
        Map<String, Boolean> resultMap = new HashMap<>();
        resultMap.put("delResult", sysUserService.removeSysUser(usrId));
        return resultMap;
    }

}
